package com.ruoyi.web.controller.workJob;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.validator.ValidatorUtils;
import com.ruoyi.worker.constant.Constant;
import com.ruoyi.worker.domain.Company;
import com.ruoyi.worker.domain.RzBlockWorker;
import com.ruoyi.worker.domain.TimeShift;
import com.ruoyi.worker.domain.WorkerInfos;
import com.ruoyi.worker.service.ITermWorkerService;
import com.ruoyi.worker.service.WorkerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName WorkerInfoController
 * @Description
 * @Author leizhang
 * Date 2021/8/25 12:10 上午
 * @Version 1.0
 **/
@RestController
@RequestMapping("/workJobApi")
public class WorkerInfoController extends BaseController {
    @Autowired
    private WorkerInfoService workerInfoService;
    @Autowired
    private ITermWorkerService termWorkerService;

    //@PreAuthorize("@ss.hasPermi('system:role:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkerInfos workerInfos) {
        startPage();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        workerInfos.setCompanyId(user.getCompanyId());
        List<WorkerInfos> workerInfo = workerInfoService.getWorkerInfos(workerInfos);
        return getDataTable(workerInfo);
    }
   /**
    * 获取临时工列表
    * @author 刘松山
    * @date 2021/9/12 17:37
    * @param workerInfos
    * @return com.ruoyi.common.core.page.TableDataInfo
    */
    @GetMapping("/listWorkerInfo")
    public TableDataInfo listWorkerInfo(WorkerInfos workerInfos) {
        startPage();
        List<WorkerInfos> workerInfo = workerInfoService.listWorkerInfo(workerInfos);
        return getDataTable(workerInfo);
    }
   /**
    * 获取临时工详情
    * @author 刘松山
    * @date 2021/9/12 18:27
    * @param id
    * @return com.ruoyi.common.core.domain.AjaxResult
    */
    @Log(title = "工人管理", businessType = BusinessType.UPDATE)
    @GetMapping("/getWorker/{id}")
    public AjaxResult getWorker(@PathVariable("id") Long id) {
        AjaxResult ajax = AjaxResult.success();
        WorkerInfos w = workerInfoService.getWorker(id);
        ajax.put(AjaxResult.DATA_TAG, w);
        return    ajax;
    }

    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PutMapping("/modifyWorkerInfo")
    public AjaxResult modifyWorkerInfo(@Validated @RequestBody WorkerInfos workerInfos)
    {
        int count = workerInfoService.countWorkerInfoByIdCard(workerInfos);
        if(count >0){
            return AjaxResult.error("身份证号已经存在了");
        }
        workerInfos.setUpdateBy(getUsername());

        return toAjax(workerInfoService.modifyWorkerInfo(workerInfos));
    }

    /**
     * 删除临时工
     */
    //@PreAuthorize("@ss.hasPermi('job:attendanceGroup:detail:delete')")
    @Log(title = "删除临时工", businessType = BusinessType.DELETE)
    @DeleteMapping("/delWorker/{id}")
    public AjaxResult delWorker(@PathVariable Long id)
    {
        return toAjax(workerInfoService.delWorkerInfo(id));
    }

    @GetMapping("/timeShiftList")
    public AjaxResult selectTimeShiftInfos(@RequestParam Map<String, Object> requestParam) {

        List<TimeShift> workerInfos = workerInfoService.selectTimeShiftInfos(requestParam);
        return AjaxResult.success(workerInfos);
    }

    @GetMapping("/companyList")
    public AjaxResult selectCompanyInfos(@RequestParam Map<String, Object> requestParam) {
        List<Company> workerInfos = workerInfoService.selectCompanyInfos(requestParam);
        return AjaxResult.success(workerInfos);
    }

//    @Log(title = "兼职管理", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@ss.hasPermi('system:role:export')")
    @GetMapping("/export")
    public AjaxResult export(WorkerInfos workerInfo) {
        List<WorkerInfos> workerInfos = workerInfoService.getWorkerInfos(workerInfo);
        ExcelUtil<WorkerInfos> util = new ExcelUtil<WorkerInfos>(WorkerInfos.class);
        return util.exportExcel(workerInfos, "员工数据");
    }

    //  @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "兼职管理_临时工黑名单", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Map<String, Object> requestParam) {
        return toAjax(workerInfoService.updateRoleStatus(requestParam));
    }

    @PutMapping("/changeWorkStatus")
    public AjaxResult changeWorkStatus(@RequestBody Map<String,Object> requestParam) {
        return toAjax(workerInfoService.updateWorkStatus(requestParam.get("orderCode").toString(),requestParam.get("status").toString()));
    }

    @Log(title = "兼职管理", businessType = BusinessType.UPDATE)
    @GetMapping("/getImg/{workerId}")
    public AjaxResult getImg(@PathVariable("workerId") int workerId) {
      return   AjaxResult.success("操作成功",workerInfoService.getImg(workerId));
    }


    /**
     * 加入黑名单_临时工
     *
     * @param beau:临时工信息
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author Hush.
     * @since 2021/12/15 17:53
     */
    @Log(title = "加入黑名单_临时工", businessType = BusinessType.UPDATE)
    @PostMapping("/addToBlacklist")
    public AjaxResult addToBlacklist(@Valid @RequestBody RzBlockWorker beau, BindingResult result) {

        if (result.hasErrors()){
            // 没通过校验
            return AjaxResult.error(ValidatorUtils.getValidationMessByBindingResult(result).toString());
        }
        beau.setWorkerType(Constant.WORKER_TYPE_L);
        RzBlockWorker info = termWorkerService.checkBlackFlag(beau);
        if (!Objects.isNull(info)) {
            return AjaxResult.error("该临时工已在黑名单中");
        }
        return toAjax(workerInfoService.updateWorkStatus(beau));
    }

}
